package com.honey.standard.writer.io;

import java.io.File;

/**
 * 备份原有的文件, 新内容覆盖文件,例如:<br />
 * 存储a.java文件,如果a.java存在那么本存储方法把 a.java变成a.2012-10-15.1.java. 新的内容覆盖 a.java文件<br />
 * 注意:<br />
 * BackupStorage 和 MultiStorage 存储方式刚好相反.
 * @author Administrator
 *
 */
class BackupStorage extends Storage {
	
	public BackupStorage(File source, boolean checked) {
		super(source, checked);
	}

	public BackupStorage(File source, String charset, boolean checked) {
		super(source, charset, checked);
	}
	
	public BackupStorage(String source, String charset, boolean checked) {
		super(source, charset, checked);
	}

	protected void setSource(File source) {
		source = backupFile(source);
		super.setSource(source);
	}
	
	private File backupFile(File source) {
		File answer = null;
		if( source.exists() ){
			File backup = IoUtility.uniqueFile( source.getParentFile(), source.getName() );
			//boolean b = source.renameTo(backup);
			IoUtility.copyFile(source, backup);
			answer = source;
		}else{
			answer = source;
		}
		return answer ;
	}
	
	@Override
	public boolean supportWrite() {
		return true;
	}	
}
